package org.agile4j.plugin.gun.xml;

import java.io.File;
import java.util.Map;

import org.agile4j.plugin.gun.helper.GunHelper;
import org.agile4j.plugin.gun.utils.ParserUtil;
import org.agile4j.plugin.gun.utils.ReplaceContainerUtil;
import org.agile4j.plugin.gun.xml.servicetype.Field;
import org.agile4j.plugin.gun.xml.servicetype.Fields;
import org.agile4j.plugin.gun.xml.servicetype.Interface;
import org.agile4j.plugin.gun.xml.servicetype.Service;
import org.agile4j.plugin.gun.xml.servicetype.ServiceType;
import org.agle4j.framework.utils.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * servcie type 文件替换
 * 
 * @author hanyx
 * @since
 */
@org.agle4j.framework.annotation.Service
public class GunServiceType {

	private static final Logger LOGGER = LoggerFactory.getLogger(GunServiceType.class);

	public boolean createTarFile(String createTarFileUrl) {
		Map<String, ServiceType> serviceTypeMap = ParserUtil.deplogyServiceType(new File(createTarFileUrl));
		if (CollectionUtil.isNotEmpty(serviceTypeMap)) {
			for (Map.Entry<String, ServiceType> serviceTypeEntry : serviceTypeMap.entrySet()) {
				ServiceType serviceType = serviceTypeEntry.getValue();
				if (CollectionUtil.isNotEmpty(serviceType.getDescriptionOrService())) {
					for (Object serviceTypeObj : serviceType.getDescriptionOrService()) {
						if (serviceTypeObj instanceof Service) {
							Service service = (Service) serviceTypeObj;
							if (ReplaceContainerUtil.containsKey(service.getId())) {
								service.setId(ReplaceContainerUtil.getReplaceValue(service.getId()));
							}
							
							if (service.getInterface() != null) {
								Interface inter = service.getInterface();
								if (inter.getInput() != null) {
									if (CollectionUtil.isNotEmpty(inter.getInput().getFieldOrFields())) {
										for (Object interObj : inter.getInput().getFieldOrFields()) {
											if (interObj instanceof Field) {
												Field field = (Field) interObj;
												replaceField(field);
											}
											if (interObj instanceof Fields) {
												Fields fields = (Fields) interObj;
												replaceFields(fields);
											}
										}
									}
								}
								if (inter.getOutput() != null) {
									if (CollectionUtil.isNotEmpty(inter.getOutput().getFieldOrFields())) {
										for (Object outerObj : inter.getOutput().getFieldOrFields()) {
											if (outerObj instanceof Field) {
												Field field = (Field) outerObj;
												replaceField(field);
											}
											if (outerObj instanceof Fields) {
												Fields fields = (Fields) outerObj;
												replaceFields(fields);
											}
										}
									}
								}
							}
						}
					}
				}
				File file = new File(serviceTypeEntry.getKey());
				String tarFilePath = file.getParent() + File.separator + GunHelper.getTarFileFormat() + file.getName(); // 目标文件路径
				ParserUtil.replaceXmlField(tarFilePath, serviceType);
			}
		}
		return true;
	}

	private void replaceField(Field field) {
		if (ReplaceContainerUtil.containsKey(field.getId())) {
			field.setId(ReplaceContainerUtil.getReplaceValue(field.getId()));
		}
		if (ReplaceContainerUtil.containsKey(field.getRef())) {
			field.setRef(ReplaceContainerUtil.getReplaceValue(field.getId()));
		}
		if (ReplaceContainerUtil.containsKey(field.getType())) {
			field.setType(ReplaceContainerUtil.getReplaceValue(field.getType()));
		}
	}

	private void replaceFields(Fields fields) {
		if (ReplaceContainerUtil.containsKey(fields.getId())) {
			fields.setId(ReplaceContainerUtil.getReplaceValue(fields.getId()));
		}

		if (CollectionUtil.isNotEmpty(fields.getFieldOrFields())) {
			for (Object fieldsObj : fields.getFieldOrFields()) {
				if (fieldsObj instanceof Field) {
					Field field = (Field) fieldsObj;
					replaceField(field);
				}
				if (fieldsObj instanceof Fields) {
					Fields fields1 = (Fields) fieldsObj;
					replaceFields(fields1);
				}
			}
		}
	}

}